package com.shenmazong.order.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.order.mapper.TbProductMapper;
import com.shenmazong.order.pojo.TbProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: TaskElasticSearch
 * @date 2022/8/4 15:36
 */

@Component
@EnableScheduling
@Slf4j
public class TaskElasticSearch {

    private Boolean syncFirst = true;

    @Autowired
    TbProductMapper tbProductMapper;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * @description 产品信息全量同步
     * @author 军哥
     * @date 2022/8/4 15:59
     * @version 1.0
     */

    @Scheduled(fixedRate = 1000)
    public void fullSyncProducts() {
        //--1 如果不是第一次运行，则放弃全量同步
        if(!syncFirst) {
           return;
        }

        //--2 创建索引
        if(!elasticsearchRestTemplate.indexOps(TbProduct.class).exists()) {
           // 创建索引
           elasticsearchRestTemplate.indexOps(TbProduct.class).create();
           // 手动创建映射
           Document mapping = elasticsearchRestTemplate.indexOps(TbProduct.class).createMapping();
           elasticsearchRestTemplate.indexOps(TbProduct.class).putMapping(mapping);
        }

        //--3 进行全量同步
        List<TbProduct> tbProducts = tbProductMapper.selectList(null);
        elasticsearchRestTemplate.save(tbProducts);

        //--4 不再运行
        log.info("fullSyncProducts....");
        syncFirst = false;
    }

    /**
     * @description 增量同步产品信息
     * @author 军哥
     * @date 2022/8/4 16:03
     * @version 1.0
     */

    @Scheduled(fixedDelay = 1000*60)
    public void partSyncProducts() {
        //--1 获取上次更新的时间
        String key = "partSyncProductsUpdateTime";
        Date lastUpdateTime = (Date)redisTemplate.opsForValue().get(key);
        Date updateTime = null;

        //--2 创建索引
        if(!elasticsearchRestTemplate.indexOps(TbProduct.class).exists()) {
            // 创建索引
            elasticsearchRestTemplate.indexOps(TbProduct.class).create();
            // 手动创建映射
            Document mapping = elasticsearchRestTemplate.indexOps(TbProduct.class).createMapping();
            elasticsearchRestTemplate.indexOps(TbProduct.class).putMapping(mapping);
        }

        //--2 获取大于上次更新时间的所有记录
        if(lastUpdateTime == null) {
            QueryWrapper<TbProduct> wrapper = new QueryWrapper<>();
            wrapper.lambda().orderByDesc(TbProduct::getUpdateTime);
            // 存入ES
            List<TbProduct> tbProducts = tbProductMapper.selectList(wrapper);
            elasticsearchRestTemplate.save(tbProducts);

            // 更新时间
            updateTime = tbProducts.get(0).getUpdateTime();
            redisTemplate.opsForValue().set(key, updateTime);
            log.info("partSyncProducts1....");
        }
        else {
            QueryWrapper<TbProduct> wrapper = new QueryWrapper<>();
            wrapper.lambda().gt(TbProduct::getUpdateTime, lastUpdateTime);
            wrapper.lambda().orderByDesc(TbProduct::getUpdateTime);
            // 存入ES
            List<TbProduct> tbProducts = tbProductMapper.selectList(wrapper);
            if(tbProducts != null && tbProducts.size() > 0) {
                elasticsearchRestTemplate.save(tbProducts);

                // 更新时间
                updateTime = tbProducts.get(0).getUpdateTime();
                redisTemplate.opsForValue().set(key, updateTime);
            }

            log.info("partSyncProducts2....");
        }
    }

}
